package flc.ast.utils;

import androidx.annotation.WorkerThread;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import flc.ast.bean.OvertimeBean;
import flc.ast.bean.OvertimeRecordBean;

public class MyRecordLoader {

    private static MyRecordLoader sInstance;
    private final List<OvertimeRecordBean> overtimeBeanList = new ArrayList<>();

    private MyRecordLoader() {

    }

    public synchronized static MyRecordLoader getInstance() {
        if (sInstance == null) {
            sInstance = new MyRecordLoader();
        }
        return sInstance;
    }

    @WorkerThread
    public List<OvertimeRecordBean> loadData(boolean forceAgainGet, List<OvertimeBean> list) {
        if (!forceAgainGet) {
            if (overtimeBeanList.size() > 0) {
                return overtimeBeanList;
            }
        }
        overtimeBeanList.clear();

        Map<String, List<OvertimeBean>> timeCategoryMap = new HashMap<>();
        for (OvertimeBean overtimeBean : list) {
            String time = TimeUtils.millis2String(overtimeBean.getDate(), "MM-dd EEEE");

            List<OvertimeBean> beanList = null;
            if (timeCategoryMap.containsKey(time)) {
                beanList = timeCategoryMap.get(time);
            } else {
                beanList = new ArrayList<>();
                timeCategoryMap.put(time, beanList);
            }

            beanList.add(overtimeBean);
        }
        Iterator<String> iterator = timeCategoryMap.keySet().iterator();
        String time = null;
        while (iterator.hasNext()) {
            time = iterator.next();
            overtimeBeanList.add(new OvertimeRecordBean(time, timeCategoryMap.get(time)));
        }
        SortUtils.getSortShotBefore(overtimeBeanList);
        return overtimeBeanList;
    }
}
